package com.whosly.mysql.protocol.packet.impl;

/**
 * @author fengyang
 * @date 2023/7/20 下午3:58
 * @see <a href="yueny09@163.com">mailTo: yueny09@163.com</a>
 */
class StringFillFormat
{

    public static final int RIGHT_ALIGN = 0;

    public static final int LEFT_ALIGN = 1;

    private static final char splitChar = ' ';

    public static String format(String s, int fillLength) {
        return format(s, fillLength, splitChar, LEFT_ALIGN);
    }

    public static String format(int i, int fillLength) {
        return format(Integer.toString(i), fillLength, splitChar, RIGHT_ALIGN);
    }

    public static String format(long l, int fillLength) {
        return format(Long.toString(l), fillLength, splitChar, RIGHT_ALIGN);
    }

    public static String format(String s, int fillLength, char fillChar, int align) {
        if (s == null) {
            s = "";
        } else {
            s = s.trim();
        }
        int charLen = fillLength - s.length();
        if (charLen > 0) {
            char[] fills = new char[charLen];
            for (int i = 0; i < charLen; i++) {
                fills[i] = fillChar;
            }
            StringBuilder str = new StringBuilder(s);
            switch (align) {
                case RIGHT_ALIGN:
                    str.insert(0, fills);
                    break;
                case LEFT_ALIGN:
                    str.append(fills);
                    break;
                default:
                    str.append(fills);
            }
            return str.toString();
        } else {
            return s;
        }
    }
}
